package com.cipta.lomob;

import org.json.JSONObject;

import tv.lomob.api.Lomob;
import tv.lomob.api.LomobResponseHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cipta.lomob.base.BaseActivity;

public class ForgotPasswordActivity extends BaseActivity{
	private Button btnForgotPassword;
	private TextView txtRegister;
	private EditText txtEditEmail;
	private ProgressDialog progressDialog;
	private AlertDialog.Builder dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forgot_password);
		
		initializeViews();
		initializeProcess();
		initializeActions();
	}
	
	@Override
	public void initializeViews() {
		// TODO Auto-generated method stub
		super.initializeViews();
		txtEditEmail = (EditText) findViewById(R.id.edt_forgot_password_email);
		txtRegister = (TextView)findViewById(R.id.txt_forgot_password_register);
		btnForgotPassword = (Button)findViewById(R.id.btn_forgot_password);
		//alert dialog
		dialog = new AlertDialog.Builder(ForgotPasswordActivity.this);
	}
	
	@Override
	public void initializeProcess() {
		// TODO Auto-generated method stub
		super.initializeProcess();
		
		String register = "Register Here";
		txtRegister.setText(register);
		txtRegister.setTextColor(getResources().getColor(R.color.comand_text_color));
	}
	
	@Override
	public void initializeActions() {
		// TODO Auto-generated method stub
		super.initializeActions();
		
		btnForgotPassword.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				requestPassword();
			}
		});
		
		txtRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RegisterActivity.toRegisterActivity(ForgotPasswordActivity.this);
			}
		});
		
		//action for alert dialog
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
	}
	
	public static void toForgotPasswordActivity(Activity activity){
		activity.startActivity(new Intent(activity, ForgotPasswordActivity.class));
	}
	
	/*
	 * function to request password
	 */
	
	private void requestPassword(){
		String email = txtEditEmail.getText().toString();
		
		if(email.isEmpty()) return;
		
		//prepare progress dialog
		progressDialog = new ProgressDialog(this);
		progressDialog.setCancelable(false);
		progressDialog.setMessage(getString(R.string.text_loading_reset_password));
		progressDialog.show();
		
		Lomob.requestPassword(email, new LomobResponseHandler() {
			
			@Override
			public void onSuccess(JSONObject result) {
				progressDialog.dismiss();
				try{
					dialog.setMessage(result.optString("message"));
					dialog.show();
					showForgetPasswordDialog();
				}catch(Exception ex){
					ex.printStackTrace();
				}
				
			}
			
			@Override
			public void onError(String message) {
				progressDialog.dismiss();
				dialog.setMessage(message);
				dialog.show();

			}
		});
	}
	
	private void showForgetPasswordDialog(){
		final Dialog dialog = new Dialog(ForgotPasswordActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_forgot_password);
		Button btnOk = (Button)dialog.findViewById(R.id.btn_dialog_forgotpassword_);
		btnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
